﻿// Author : Create By Argo
// MSN    : Argo@live.ca
// Version: 1.0
// Date   : 2007-12-29 at Home
// Summary:
// Help   : 
// Note   :

/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace("Longbow.UI");

Longbow.UI.HyperlinkBehavior = function(element) {
    /// <summary>构造函数</summary>
    /// <param name="element" type="domElement">HTMLElement A Object</param>
    Longbow.UI.HyperlinkBehavior.initializeBase(this, [element]);    
    
    var e = Function._validateParams(arguments, [
        {name: "element", domElement: true}
    ]);
    if (e) throw e;
        
    if (element.tagName !== "A")
        throw Error.argument("element", "the element must be a link"); 
        
    this._onClickHandler = null;
}

Longbow.UI.HyperlinkBehavior.prototype = {
    initialize: function() {
        Longbow.UI.HyperlinkBehavior.callBaseMethod(this, 'initialize');
        
        if (this._onClickHandler === null) {
            this._onClickHandler = Function.createDelegate(this, this._raiseOnClick);
            $addHandler(this.get_element(), "click", this._onClickHandler);
        }
    },
    
    _raiseOnClick: function(eventObject) {
        var handler = this.get_events().getHandler("click");
        if (handler) {
            handler(eventObject, new Sys.PropertyChangedEventArgs("click"));
        }
    },
    
    add_Click: function(handler) {
        if (handler)
            this.get_events().addHandler(this._beforeClickEventName, handler);
    },
    
    remove_Click: function(handler) {
        if (handler)
            this.get_events().removeHandler(this._beforeClickEventName, handler);
    },
    
    dispose: function() {
        //Add custom dispose actions here
        if (this._onClickHandler != null) {
            $removeHandler(this.get_element(), "click", this._onClickHandler);
        }
        this._onClickHandler = null;
               
        Longbow.UI.HyperlinkBehavior.callBaseMethod(this, 'dispose');
    }
}

Longbow.UI.HyperlinkBehavior.registerClass("Longbow.UI.HyperlinkBehavior", Sys.UI.Behavior);

$addParameter = Longbow.UI.addParameterToQueryString = function(url, paramName, paramValue) {
    /// <summary>在指定的Url上增加指定的Key/Value键值对。</summary>
    /// <param name="url" type="String">url地址字符串。</param>
    /// <param name="paramName" type="String">需要添加的QueryString Key名称。</param>
    /// <param name="paramValue" type="Object">需要添加的QueryString Key值。</param>
    /// <returns type="String">返回修正过的URL字符串。</returns>    
    var e = Function._validateParams(arguments, [
        {name : "url", type : String},
        {name : "paramName", type : String},
        {name : "paramValue"}
    ]);
    if (e) throw e;
    
    url = $removeParameter(url, paramName);
    url += url.indexOf("?") == -1 ? "?" : "&";
    url = String.format("{0}{1}={2}", url, paramName, encodeURIComponent(paramValue));
            
    return url;
}

$removeParameter = Longbow.UI.removeParameterFromQueryString = function(url, paramName) {
    /// <summary>在指定的Url上删除指定的Key值。</summary>
    /// <param name="url" type="String">url地址字符串。</param>
    /// <param name="paramName" type="String">需要删除的QueryString Key名称。</param>
    /// <returns type="String">返回修正过的URL字符串。</returns>    
    var e = Function._validateParams(arguments, [
        {name : "url", type : String},
        {name : "paramName", type : String}
    ]);
    if (e) throw e;
    
    var search = url.split("?");
    if (search.length === 2 && paramName.length > 0)
    {
        var params = search[1].split("&");
        for(var i = 0; i < params.length; i++)
        {
            if (params[i].indexOf(String.format("{0}=", paramName)) > -1)
            {
                if (params[i].indexOf(String.format("{0}=", paramName)) > -1) {
                    if (i > 0) {
                        return i === params.length - 1 ? url.replace(String.format("&{0}", params[i]), "") : url.replace(String.format("{0}&", params[i]), "");
                    }
                    else {
                        return url.replace(String.format("?{0}", params[i]), "");
                    }
                }
            }
        }
    }
    
    return url;
}

$getParameter = Longbow.UI.getParameterFromQueryString = function(url, paramName) {
    /// <summary>在指定Key的要获取Url上值。</summary>
    /// <param name="url" type="String">url地址字符串。</param>
    /// <param name="paramName" type="String">需要获得的QueryString Key名称。</param>
    /// <returns type="String">返回指定Key键对应的值。</returns>    
    var search = url.split("?");
    if (search.length === 2 && paramName.length > 0)
    {
        var params = search[1].split("&");
        for(var i = 0; i < params.length; i++)
        {
            if (params[i].indexOf(String.format("{0}=", paramName)) > -1)
            {
                return params[i].replace(String.format("{0}=", paramName), "");
            }
        }
    }
    return "";
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
